package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {


    @Override
    public Boolean saveSetmeal(SetmealDTO setmealDTO) {

        boolean flag = saveOrUpdate(setmealDTO);
        Integer id = setmealDTO.getId();
        //  中间表 t_setmeal_checkgroup  录入数据
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        for (Integer checkgroupId : checkgroupIds) {
            baseMapper.addSetmealAndGroupInfo(id,checkgroupId);
        }
        //  set  集合 移除    图片信息   ---set   存放垃圾图片
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDTO.getImg());
        return flag;  //
    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //  条件构造出来   Wrapper对象
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<Setmeal>();
        Page<Setmeal> page = null;
        if(StringUtils.isBlank(queryPageBean.getQueryString())){
            page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()));
        }else{
            queryWrapper.like("HELPCODE",queryPageBean.getQueryString());
            //   sql   + 追加  sql  where  name like  %?%
            page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }
        PageResult  result = new PageResult(page.getTotal(),page.getRecords());
        return result;
    }

    /**
     *   该方法： xxl-job 框架调用
     */
    @Override
    public void clearOssImg() {
        //    清理  oss  垃圾图片   该方法 有  xxl-job 去调用
        //  1.  获取 set 集合 所有图片信息
        Set<String> members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        if(members.size()!=0){
            for (String uuidfilename : members) {
                //   遍历 图片  判断 redis 该图片有没有存在
                boolean existsKey = RedisUtil.existsKey(RedisConstant.SINGLE_PIC + uuidfilename);
                if(existsKey==false){
                    //   set集合中没找到  redis 对应得图片信息  那么通知 oss删除图片即可
                    AliyunUtils.deleteFile(uuidfilename);
                    System.out.println("-----阿里云删除垃圾图片一次----");
                    //  set    集合  垃圾图片 删除
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);
                }
            }
        }

    }

    @Override
    public SetmealVO findSetmealVOPyId(String id) {
        SetmealVO setmealVO = baseMapper.findSetmealVOPyId(id);
        System.out.println("setmealVO = " + setmealVO);
        List<Map> checkGroupMap = baseMapper.findCheckGroupByIds(id);
        setmealVO.setCheckGroupsMap(checkGroupMap);
        //checkGroupByIds = [{ID=5, NAME=一般检查}, {ID=6, NAME=视力色觉}, {ID=7, NAME=血常规}]
        List<Map> checkItemMap = null;
        for (Map map : checkGroupMap) {
            checkItemMap = baseMapper.findCheckItemByIds(map.get("ID"));
            map.put("checkItemList",checkItemMap);
        }
        System.err.println(checkGroupMap);
        return setmealVO;
    }


}